/* $Header: method_ui.c,v 2.2 01/10/02 10:32:12 przybyls Exp $ */

/************************************************************************
        File: method_ui.c

        Author: John DeGood

        Contents: Method user interface functions.

        Copyright (c) 1992 Hewlett-Packard Co.
************************************************************************/

#include <typedefs.h>
#include <error.h>
#include <p_wkfile.h>
#include <wkfile.h>
#include <cmd_set.h>
#include <mio.h>
#include <i_op.h>
#include <method.h>
#include <clock_ui.h>
#include <hostutil.h>
#include <wkfutil.h>
#include <zone_config.h>
#include <pcb_defs.h>
#include <ramps.h>
#include <zone_status.h>
#include <status.h>
#include <meth_config.h>

/***********************************************************************/

UI_ERR MethodExists(U_INT8 n)
/*
**      Test if method exists.
**
**      RETURNS:  OK             - method exists
**                NOT_INSTALLED  - error:  method number empty
**                INVALID_PARAM  - error:  method number invalid
*/
{
        /* is method number invalid? */
        if (n < 1 || n > MAX_STORED_WKFILE)
        {
                return INVALID_PARAM;
        }

        /* is method empty? */
        else if (Stored_Wkfile[n - 1].length == 0)
        {
                return NOT_INSTALLED;
        }
        else
        {
                return OK;
        }
}

/***********************************************************************/

UI_ERR CheckMethod(U_INT8 n)
/*
**      Check method config against current instrument config.
**
**      RETURNS:  OK             - configuration matches
**                NOT_COMPATIBLE - configuration doesn't match
**                NOT_INSTALLED  - error:  method number empty
**                INVALID_PARAM  - error:  method number invalid
*/
{
        UI_ERR status;

        /* is method number valid and non-empty? */
        if ((status = MethodExists(n)) == OK)
        {
                status = CheckWorkfile(Stored_Wkfile_Data[n - 1],
                                       Stored_Wkfile[n - 1].length,
                                       Stored_Wkfile[n - 1].crc,
                                       BOTH,
                                       INTERNAL_BUFFER-2);
        }

        return status;
}

/***********************************************************************/

UI_ERR DcLoadMethod(U_INT8 n, U_INT16 port)
/*
**      Replace Active_Wkfile with previously stored workfile.
**
**      RETURNS:  OK             - method loaded
**                NOT_INSTALLED  - error:  method number empty
**                INVALID_PARAM  - error:  method number invalid
*/
{
        UI_ERR status;

        /* is method number valid and non-empty? */
        if ((status = MethodExists(n)) == OK)
        {
                InstallWorkfile(Stored_Wkfile_Data[n - 1],
                                Stored_Wkfile[n - 1].length,
                                Stored_Wkfile[n - 1].crc,
                                BOTH, INTERNAL_BUFFER-2);

                UpdateMethodModified (port);

/* REVISIT */
#if 0
                /* Output ALS parameters if there are any: */
                /* (uses Active_Wkfile copy)               */
                if (Active_Wkfile.have_als_wkfile)
                {
                   status = SetMethodAlsParms();
                }
#endif
        }

        return status;
}

UI_ERR LoadMethod(U_INT8 n)
{
   return DcLoadMethod(n, INTERNAL_BUFFER);
}

/***********************************************************************/

UI_ERR StoreMethod(U_INT8 n)
/*
**      Store Active_Wkfile.
**
**      RETURNS:  OK             - method loaded
**                TABLE_FULL     - out of memory
**
**      WARNING:  assumes method number is valid and empty!
*/
{
        INT16 length;

        /* REVISIT - need to use real max length later */
/*      length = CreateWkfile((BIT8 *)Stored_Wkfile_Data[n - 1], 2499, Stored_Wkfile[n - 1].crc, BOTH);  */

        length = CreateWkfile((BIT8 *)Stored_Wkfile_Data[n - 1],USR_STORED_WKFILE_MAX , Stored_Wkfile[n - 1].crc, BOTH, INTERNAL_BUFFER-2);

        /* record date and time stored */
        INTERRUPTS_OFF
        Stored_Wkfile[n - 1].dateTime = DateTime;
        INTERRUPTS_ON

/* REVISIT */
#if 0
        /* update stored method header */
        Stored_Wkfile[n - 1].header.version = 1;

        Stored_Wkfile[n - 1].header.wkfileCRC = crc16(
                (U_INT8 *)&Stored_Wkfile[n - 1].config,
                sizeof(WKFILE) - sizeof(WKFILE_HEADER));
#endif

        if (length < 0)
        {
                Stored_Wkfile[n - 1].length = 0;
                return TABLE_FULL;
        }
        else
        {
                Stored_Wkfile[n - 1].length = length;
                return OK;
        }
}

/***********************************************************************/

void DeleteMethod(U_INT8 n)
/*
        Delete Saved_Wkfile number.

        WARNING:  MethodExists() must have been called and retuned OK.
*/
{
        /* mark stored method as empty */
        Stored_Wkfile[n - 1].length = 0;
}

/***********************************************************************/

DATETIME GetMethodDateTime (U_INT8 n)
/*
        Get time & date of stored workfile.

*/
{
        if (MethodExists(n) == OK)
        {
           return Stored_Wkfile[n - 1].dateTime;
        }
        else
        {
           return Frozen_Date_Time;
        }
}

/***********************************************************************/

INT16 GetMethodName (BIT8* string, U_INT8 meth)
{
   INT8  i;

   i = 0;

   if (meth == 0)    /* return Active Workfile Name for method# 0 */
   {
      while ((i < 20) && (Active_Wkfile.name[i] != 0))
      {
         string[i]  = Active_Wkfile.name[i];
         i++;
      }
   }
   else
   {
      if (MethodExists(meth) == OK)
      {
         while ((i < 20) && (Stored_Wkfile[meth-1].name[i] != 0))
         {
            string[i]  = Stored_Wkfile[meth-1].name[i];
            i++;
         }
      }
   }

   return i;
}

void LetMethodName (BIT8* string)
{
   INT8 i;

   i = 0;

   while ( (string[i] != 0) && (i < 20) )
   {
      gccw.name[i] = string[i];
      i++;
   }

   gccw.name[i] = 0;

}

void DCSetMethodName (BIT8* string, BIT8 clear_meth_dirty)
{
   INT8 i;
   BIT8 dirty_laundry;

   i = 0;

   while ( (string[i] != 0) && (i < 20) )
   {
      Active_Wkfile.name[i] = string[i];
      i++;
   }

   dirty_laundry = Method_Dirty;   /* preserve state of Method_Dirty in case cmd doesn't want it changed */

/* LogSetptChange (METHOD_NAME, 0, 0); */
   Active_Wkfile.name[i] = 0;
   Current_Method = 0;         /* no longer matches stored method */

   if (clear_meth_dirty)
   {
      Method_Dirty   = FALSE;     /* Define newly named method as "clean" */
   }
   else
   {
      Method_Dirty   = dirty_laundry;
   }
}

void SetMethodName (BIT8* string)
{
   DCSetMethodName (string, TRUE);
}
